package com.android.clinic.doctors.app.utils;

import android.Manifest;
import android.content.Context;

import com.android.clinic.doctors.app.R;

import java.util.ArrayList;
import java.util.List;

import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;

/**
 * Created by cheng on 2018/6/26.
 */
public class PermissionUtils {

    public static void applyScan(Context mContext, final CallBack mCallBack) {
        //请求单个权限 同意后单个走onGuarantee()
        HiPermission.create(mContext).checkSinglePermission(Manifest.permission.CAMERA, new PermissionCallback() {
            @Override
            public void onClose() {

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onDeny(String permission, int position) {
                mCallBack.onDeny();
            }

            @Override
            public void onGuarantee(String permission, int position) {
                mCallBack.onGuarantee();
            }
        });
    }

    public static void applyLocation(Context mContext, final CallBack mCallBack) {

        HiPermission.create(mContext).checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION, new PermissionCallback() {
            @Override
            public void onClose() {

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onDeny(String permission, int position) {
                mCallBack.onDeny();
            }

            @Override
            public void onGuarantee(String permission, int position) {
                mCallBack.onGuarantee();
            }
        });
    }

    public static void applyCamera(Context mContext, final CallBack mCallBack) {
        //请求多个权限 同意后走onFinish()
        List<PermissionItem> permissionItems = new ArrayList<>();
        permissionItems.add(new PermissionItem(Manifest.permission.CAMERA, "相机", R.drawable.permission_ic_camera));
        permissionItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "存储卡", R.drawable.permission_ic_storage));
        HiPermission.create(mContext).permissions(permissionItems)
                .checkMutiPermission(new PermissionCallback() {
                    @Override
                    public void onClose() {

                    }

                    @Override
                    public void onFinish() {
                        mCallBack.onGuarantee();
                    }

                    @Override
                    public void onDeny(String permission, int position) {
                        mCallBack.onDeny();
                    }

                    @Override
                    public void onGuarantee(String permission, int position) {

                    }
                });
    }

    public static void applyUpdate(Context mContext, final CallBack mCallBack) {
        //请求多个权限 同意后走onFinish，单个走onGuarantee
        List<PermissionItem> permissionItems = new ArrayList<PermissionItem>();
        permissionItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "存储卡", R.drawable.permission_ic_storage));
        permissionItems.add(new PermissionItem(Manifest.permission.ACCESS_FINE_LOCATION, "位置", R.drawable.permission_ic_location));
        HiPermission.create(mContext)
                .permissions(permissionItems)
                .checkMutiPermission(new PermissionCallback() {
                    @Override
                    public void onClose() {
                    }

                    @Override
                    public void onFinish() {
                        mCallBack.onGuarantee();
                    }

                    @Override
                    public void onDeny(String permission, int position) {
                        mCallBack.onDeny();
                    }

                    @Override
                    public void onGuarantee(String permission, int position) {
                    }
                });
    }

    public static void applyCallPhone(Context mContext, final CallBack mCallBack) {
        HiPermission.create(mContext).checkSinglePermission(Manifest.permission.CALL_PHONE, new PermissionCallback() {
            @Override
            public void onClose() {

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onDeny(String permission, int position) {
                mCallBack.onDeny();
            }

            @Override
            public void onGuarantee(String permission, int position) {
                mCallBack.onGuarantee();
            }
        });
    }

    public static void applyReadPhone(Context mContext, final CallBack mCallBack) {
        HiPermission.create(mContext).checkSinglePermission(Manifest.permission.READ_PHONE_STATE, new PermissionCallback() {
            @Override
            public void onClose() {

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onDeny(String permission, int position) {
                mCallBack.onDeny();
            }

            @Override
            public void onGuarantee(String permission, int position) {
                mCallBack.onGuarantee();
            }
        });
    }

    public static void applyStorage(Context mContext, final CallBack mCallBack) {
        HiPermission.create(mContext).checkSinglePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, new PermissionCallback() {
            @Override
            public void onClose() {

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onDeny(String permission, int position) {
                mCallBack.onDeny();
            }

            @Override
            public void onGuarantee(String permission, int position) {
                mCallBack.onGuarantee();
            }
        });
    }

    public interface CallBack {
        void onGuarantee();

        void onDeny();
    }

}
